package com.kaiwukj.android.ufamily.mvp.ui.page.home.home.active;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.FragmentScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.ActiveParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ActiveResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.contract.HomeV20Contract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

@FragmentScope
public class ActivePresenter extends BasePresenter<HomeV20Contract.Model, HomeV20Contract.ActiveView> {

    public static final int JOIN = 1;//加入
    public static final int UN_JOIN = 2;//取消加入

    @Inject
    RxErrorHandler errorHandler;

    @Inject
    public ActivePresenter(HomeV20Contract.Model model, HomeV20Contract.ActiveView view) {
        super(model, view);
    }

    public void fetchActiveList(ActiveParams params) {
        mModel.fetchActive(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<ActiveResult>>() {
                    @Override
                    public void onSuccess(@NonNull ListResp<ActiveResult> result) {
                        mRootView.onFetchActive(result.getList());
                    }
                });
    }

    public void fetchActiveDetail(int id) {
        mModel.getActiveDetail(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ActiveResult>(errorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ActiveResult result) {
                        mRootView.onFetchActiveDetail(result);
                        mRootView.hideLoading();
                    }
                });
    }

    public void fetchActiveMy(int page, int pageSize) {
        mModel.fetchMyActive(page, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<ActiveResult>>(errorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<ActiveResult> result) {
                        mRootView.onFetchActive(result.getList());
                    }
                });
    }

    public void join(int id) {
        mModel.join(id)
                .doOnSubscribe(act -> mRootView.showLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(errorHandler, mRootView, JOIN) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                    }
                });
    }

    public void unJoin(int id) {
        mModel.unJoin(id)
                .doOnSubscribe(act -> mRootView.showLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(errorHandler, mRootView, UN_JOIN) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                    }
                });
    }

}
